package client.comm.baoding.ui

import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.Observer
import client.comm.commlib.dialog.ValidatePwdDialog
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.baoding.R
import client.comm.baoding.ui.vm.TxViewModel
import client.comm.baoding.api.bean.UserInfo
import java.util.*
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.databinding.ActivityTishuiBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.vm.TiShuiViewModel
import client.comm.commlib.dialog.CameraOrSelectDialog

/**
 * 提税
 */
class TiShuiActivity : BaseMiddleActivity<TiShuiViewModel, ActivityTishuiBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_tishui
    }

    var timerTask: TimerTask? = null
    var timer: Timer? = null
    override fun initView() {
        binding?.apply {
            event = this@TiShuiActivity
            vm = viewModel
            StoreUtil.getBean<UserInfo>(this@TiShuiActivity)?.let {
                viewModel.userInfo.value = it
                inconme = it.income_tx.toString()
            }
        }
    }

    override fun observerUI() {
        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    startActivity(Intent(this@TiShuiActivity, TiShuijlActivity::class.java))
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })

        viewModel.codeRet.observe(this, {
            it?.apply {
                if (isSuccess) {
                    toast("获取验证码成功")

                    timerTask?.let { it.cancel() }
                    timer?.let { it.cancel() }

                    viewModel.getCodeEnable.postValue(false)

                    timerTask = object : TimerTask() {
                        var count = 60
                        override fun run() {
                            count--;
                            viewModel.getCodeTxt.postValue("${count}秒后获取")
                            if (count <= 0) {
                                viewModel.getCodeEnable.postValue(true)
                                viewModel.getCodeTxt.postValue("获取验证码")
                                timerTask!!.cancel()

                            }
                        }
                    }
                    timer = Timer().apply { schedule(timerTask, 0, 1000) }

                } else {
                    toast(it.message)
                    viewModel.getCodeEnable.value = true
                    viewModel.getCodeTxt.value = "获取验证码"
                }

            }
        })

    }

    override fun initViewModel(): TiShuiViewModel = createViewModel()
    override fun onDestroy() {

        timerTask?.let { it.cancel() }
        timer?.let { it.cancel() }

        super.onDestroy()
    }

    fun ok() {
        if (viewModel.imgOne.value == null) {
            toast("请选择图片后再上传")
            return
        }

        if (TextUtils.isEmpty(viewModel.txNum.value) || viewModel.txType.value!! < 0 || viewModel.smscode.value.isNullOrEmpty()) {
            toast("所有不能为空")
            return
        }
        viewModel.ok(this)

//        ValidatePwdDialog(this).apply {
//            title = "需要验证支付密码"
//            ok = {
//                viewModel.ok(it)
//            }
//
//        }.show()
    }

    fun jl() {
        startActivity(Intent(this, TiShuijlActivity::class.java))
    }


    fun imgSelect() {
        CameraOrSelectDialog(this).apply {

            suc = {
                viewModel.imgOne.value = it
                mImgLoader.load(it).into(this@TiShuiActivity.binding.img)
            }
        }.show()
    }


    fun cancle() {
        MsgTitleDialog(this).apply {
            title = "是否撤销订单?"
            cancleTxt = "关闭"
            okTxt = "撤销"
            ok = {
                viewModel.cancle()
            }
        }.show()
    }

}